package com.example.kyyl.http;

import android.content.Context;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by on 2018/7/19.
 */
public class HttpManager {
    private Context context;
    private static final int READ_TIME_OUT = 7200;//25
    private static final int CONNECT_TIME_OUT = 7200;//25
    private ApiService mApiService;
    private HttpManager() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(HttpLoggingInterceptor.Logger.DEFAULT);//打印retrofit日志
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                .addInterceptor(loggingInterceptor)
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                //.addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl("http://ziboinout.xywuhe.com/WebService.asmx/")
                .build();
        mApiService = retrofit.create(ApiService.class);
    }
    private static HttpManager instanse = null;
    public static HttpManager getInstance() {
        if (instanse == null) {
            synchronized (HttpManager.class) {
                if (instanse == null) {
                    return new HttpManager();
                }
            }
        }
        return instanse;
    }
    public void get(String url, Map<String, String> params, Subscriber<String> subscriber) {
        Observable<String> observable = mApiService.get(url, params);
        call(observable, subscriber);
    }
    public void get(String url, Subscriber<String> subscriber) {
        Observable<String> observable = mApiService.get(url);
        call(observable, subscriber);
    }
    public void post(String url, Map<String, String> params, Subscriber<String> subscriber) {
        Observable<String> observable = mApiService.post(url, params);
        call(observable, subscriber);
    }
    public void post(String url, Subscriber<String> subscriber) {
        Observable<String> observable = mApiService.post(url);
        call(observable, subscriber);
    }
    public void upload(String url, Map<String, String> params, MultipartBody.Part file, Subscriber<String> subscriber) {
        Observable<String> observable = mApiService.upload(url, params, file);
        call(observable, subscriber);
    }
    private void call(Observable<String> observable, Subscriber<String> subscriber) {
        observable.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }
}
